package com.moana.buyTicket.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;

import com.moana.buyTicket.entity.ResultList;
import com.moana.buyTicket.search.BaseSearch;

public abstract class BaseAction {
		/**
		 * 获取列表数量
		 * @param cond
		 * @return
		 */
		protected abstract Integer getListCount(Object cond);
		/**
		 * 获取列表集
		 * @param cond
		 * @return
		 */
		protected abstract List<Object> getListData(Object cond);
		/**
		 * 获取并返回结果
		 * @return
		 */
		protected ResultList getList(HttpServletRequest request,Object cond) {
			BaseSearch base = (BaseSearch) cond;
			Integer page= base.getPage();
			RequestMapping mapping = this.getClass().getAnnotation(RequestMapping.class);
			String url=mapping.value()[0]+"?operate="+request.getParameter("operate");
			String sort_page=request.getParameter("sort_page");
			if (sort_page!=null) {
				cond=request.getSession().getAttribute(url+"_cond");
				base = (BaseSearch) cond;
				base.setPage(page);
			}else{
				request.getSession().setAttribute(url+"_cond", cond);
			}	
			url+="&sort_page=true&page=";
			ResultList result =new ResultList();
			result.setCond(cond);
			
			
			//如果有指定的searchAction，则用指定的searchAction查询数据
			if(base.getSearchAction()!=null)
			{
				result.setCount(base.getSearchAction().getListCount(cond));
				result.setList(base.getSearchAction().getListData(cond));
			}
			else
			{
				result.setCount(getListCount(cond));
				result.setList(getListData(cond));
			}
			

			result.setPages((int)Math.ceil(Double.valueOf(result.getCount())/Double.valueOf(base.getLimitSize())));
			request.setAttribute("data", result);
			return result;
		}
}
